class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
        this.prev = null
    }
}

class DoubleLinkStack {
    size = 0
    head = null
    tail = null

    push(data) {
        const node = new Node(data);
        if (!data) {
            return 
        }
        if (this.size === 0) {
            this.head = node
            this.tail = node
        }
        if (this.tail) {
            this.tail.next = node
            node.prev = this.tail
            this.tail = node
        }
        return this.size++
    }
    

    pop() {
        if (this.size === 0) {
            return 
        }
        const item = this.tail.data
        if (this.size === 1) {
            this.head = null
            this.tail = null
        }else {
            this.tail = this.tail.prev
            this.tail.next = null
        }
        
        this.size--
        return item
    }

    getSize() {
        return this.size
    }

    // 遍历
    print() {
        if (this.size === 0) {
            console.log("空栈");
            return 
        }
        let node = this.head
        while(node) {
            console.log(node.data)
            node = node.next
        }
    }
}

const doubleLinkStack = new DoubleLinkStack()

doubleLinkStack.push(1)
doubleLinkStack.push(2)
doubleLinkStack.push(3)
doubleLinkStack.print()
console.log("当前的长度为：", doubleLinkStack.getSize());

doubleLinkStack.pop()
doubleLinkStack.print()
console.log("删除后的栈长为：",doubleLinkStack.getSize());
console.log("删除的元素是", doubleLinkStack.pop());
doubleLinkStack.print()
console.log("删除的元素是", doubleLinkStack.pop());
doubleLinkStack.print()










